package com.HuangJiaQing.www.controller;

import com.HuangJiaQing.www.model.Marketers;
import com.HuangJiaQing.www.model.WareHouse;
import com.HuangJiaQing.www.service.GoodsService;
import com.HuangJiaQing.www.service.WareHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author HONOR
 * @date 2022/8/5 17:53
 */
@Controller
public class WareHouseController {
    @Autowired
    private WareHouseService wareHouseService;
    @Autowired
    private GoodsService goodsService;

    @RequestMapping(value = "/toWareHouse", method = RequestMethod.GET)
    public String toWareHouse(HttpSession session, Model model) {
        Marketers marketers = (Marketers) session.getAttribute("marketers");
        Integer marketersId = marketers.getMarketersId();
        List<WareHouse> wareHouses = wareHouseService.selectWareHouseByMarketersId(marketersId);
        model.addAttribute("warehouses", wareHouses);
        return "warehouse";
    }

    @RequestMapping(value = "/toAddWareHouse", method = RequestMethod.GET)
    public String toAddWareHouse() {
        return "addWareHouse";
    }

    @RequestMapping(value = "addWareHouse", method = RequestMethod.POST)
    public String addWareHouse(WareHouse wareHouse, HttpSession session, MultipartFile photo) {
        Marketers marketers = (Marketers) session.getAttribute("marketers");
        wareHouse.setMarketerId(marketers.getMarketersId());
        String s = goodsService.fileUp(photo);
        wareHouse.setAvatar(s);
        wareHouseService.insertWareHouse(wareHouse);
        return "redirect:/toAddWareHouse";
    }

    @RequestMapping(value = "/addGoods/{id}", method = RequestMethod.POST)
    @ResponseBody
    public String addGoods(@PathVariable("id") Integer id) {
        Boolean b = wareHouseService.insertGoodsById(id);
        if (b) {
            return "success";
        } else {
            return "false";
        }
    }

    @RequestMapping(value = "/deleteWareHouse/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public String deleteWareHouse(@PathVariable("id") Integer id) {
        wareHouseService.deleteWareHouseById(id);
        return "success";
    }

    @RequestMapping(value = "warehouseSearch", method = RequestMethod.GET)
    public String searchWareHouse(String search, Model model) {
        List<WareHouse> wareHouses = wareHouseService.searchWareHouse(search);
        model.addAttribute("warehouses", wareHouses);
        return "searchWareHouse";
    }

    @RequestMapping(value = "toForbid", method = RequestMethod.GET)
    public String toForbid(Model model) {
        List<WareHouse> wareHouses = wareHouseService.selectWareHouseForbid();
        model.addAttribute("warehouses", wareHouses);
        return "forbid";
    }

    @RequestMapping(value = "/toUpdateWareHouse/{id}", method = RequestMethod.GET)
    public String toUpdateWareHouse(@PathVariable("id") Integer id, Model model) {
        WareHouse wareHouse = wareHouseService.selectWareHouseById(id);
        model.addAttribute("warehouse", wareHouse);
        return "correct";
    }

    @RequestMapping(value = "/updateWareHouse")
    public String updateWareHouseForbid(WareHouse wareHouse, MultipartFile photo) {
        if (!photo.isEmpty()) {
            String s = goodsService.fileUp(photo);
            wareHouse.setAvatar(s);
        }
        wareHouse.setIsForbidWarehouse(2);
        wareHouseService.updateWareHouse(wareHouse);
        return "redirect:/toForbid";
    }

    @RequestMapping(value = "/updateWareHouseAgree/{id}", method = RequestMethod.PUT)
    public String agreeAudit(@PathVariable("id") Integer id) {
        WareHouse wareHouse = new WareHouse();
        wareHouse.setWarehouseId(id);
        wareHouse.setIsForbidWarehouse(0);
        wareHouseService.updateWareHouse(wareHouse);
        return "redirect:/toAudit";
    }

    @RequestMapping(value = "/updateWareHouseDisagree/{id}", method = RequestMethod.PUT)
    public String disagreeAudit(@PathVariable("id") Integer id) {
        WareHouse wareHouse = new WareHouse();
        wareHouse.setWarehouseId(id);
        wareHouse.setIsForbidWarehouse(1);
        wareHouseService.updateWareHouse(wareHouse);
        return "redirect:/toAudit";
    }

    @RequestMapping(value = "/toScheduledTasks/{id}", method = RequestMethod.GET)
    public String toScheduledTasks(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("id", id);
        return "scheduledTasks";
    }

    @RequestMapping(value = "/scheduledTasks", method = RequestMethod.POST)
    public String scheduledTasks(Integer id, String time) {
        wareHouseService.scheduledTasks(id, time);
        return "redirect:/toWareHouse";
    }
}
